package com.wuhunyu.servicebase.exception;

import com.wuhunyu.commonutils.result.CommonResult;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 *
 * @author wuhunyu
 * @version 1.0
 * @date 2021-01-13 14:25
 */
@ControllerAdvice
@Slf4j
public class GlobalException {

    /**
     * 最大的异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public CommonResult exception(Exception e) {
        log.error("最大的异常：{}",e.getMessage());
        return CommonResult.error("系统资源繁忙");
    }

    /**
     * 空指针异常
     * @return
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public CommonResult exception(NullPointerException e) {
        log.error("空指针异常:{}",e.getMessage());
        return CommonResult.error("空指针异常");
    }

    /**
     * 自定义空值异常
     * @param e
     * @return
     */
    @ExceptionHandler(NullException.class)
    @ResponseBody
    public CommonResult exception(NullException e) {
        log.error("空值异常:{}",e.getMessage());
        return CommonResult.error(e.getMessage());
    }

}
